﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Xml;

namespace WABDCUI
{
    public partial class ReciteSelectPanel : UserControl
    {

        public ReciteSelectPanel()
        {
            InitializeComponent();
            this.labReciteInfo.Text = String.Format(
                @"{0}本课本，{1}个课程，{2}个单词",
                BookDao.count(), LessonDao.count(), WordDao.count()
            );
        }

        private void reciteListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Recite recite = null;
            if (this.reciteListView.SelectedIndices.Count > 0)
            {
                int index = this.reciteListView.SelectedIndices[0];
                string name = this.reciteListView.Items[index].Text;
                recite = ReciteDao.findByName(name);
                if (recite != null)
                {
                    this.labReciteInfo.Text = recite.ToString();
                }
            }
        }

        new public void Show()
        {
            //initalize reciteListView
            this.reciteListView.BeginUpdate();
            this.reciteListView.Items.Clear();
            foreach (Recite recite in ReciteDao.findAll())
            {
                ListViewItem item = new ListViewItem(new string[] {
                    recite.Name,
                    String.Format("{0}", recite.NumberOfWords),
                    recite.AccessTime.ToString("s").Replace('T', ' ')
                });
                this.reciteListView.Items.Add(item);
            }
            this.reciteListView.EndUpdate();
            base.Show();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Recite recite = null;
            if (this.reciteListView.SelectedIndices.Count > 0)
            {
                int index = this.reciteListView.SelectedIndices[0];
                string name = this.reciteListView.Items[index].Text;
                recite = ReciteDao.findByName(name);
                if (recite != null)
                {
                    ReciteDao.remove(recite);
                    this.reciteListView.Items.RemoveAt(index);
                }
            }
        }
    }
}
